package com.example.mainstream.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.myapp.activity.GoodsCarActivity.ClearCarThread;
import com.myapp.activity.GoodsCarActivity.DeleteCarItemThread;
import com.myapp.activity.GoodsCarActivity.GoodsCarListThread;
import com.myapp.adapter.GoodsCarListAdapter;
import com.myapp.adapter.OrderListAdapter;
import com.myapp.common.HttpUtil;
import com.myweb.domain.GoodsCar;
import com.myweb.domain.UserOrder;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class OrderActivity extends ActionBarActivity {
	
	ListView listView;

	List<Map<String, Object>> orderlist1;

	private OrderListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_order_list);

		listView = (ListView) findViewById(R.id.order_list);// ��ȡ�б�Ԫ��

		orderlist1 = new ArrayList<Map<String, Object>>();

		adapter = new OrderListAdapter(this, orderlist1);// ������

		listView.setAdapter(adapter);

		Thread orderListThread = new Thread(new OrderListThread());

		orderListThread.start();

	}

	

	class OrderListThread implements Runnable {

		public void run() {
			try {

				JSONObject paraObj = new JSONObject();

				paraObj.put("action", "list");

				MyApplication myApp = (MyApplication) getApplication(); // ����Զ����Ӧ�ó���MyApp

				paraObj.put("userid", String.valueOf(myApp.getUser().getId()));

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"UserOrderService");

				if (!result.equals("")) {

					List<UserOrder> orderList = JSONObject.parseArray(result,
							UserOrder.class);

					ArrayList<Map<String, Object>> orderListMap = new ArrayList<Map<String, Object>>();

					for (UserOrder order : orderList) {

						Map<String, Object> map = new HashMap<String, Object>();

						map.put("id", order.getId());
						
						map.put("ordernum", order.getOrdernum());
						
						map.put("sumprice", order.getSumprice());

						if(order.getState().equals("0"))
						{
							map.put("state","���µ�");
						}
						else if(order.getState().equals("1"))
						{
							map.put("state","������");
						}
						else if(order.getState().equals("2"))
						{
							map.put("state","�����");
						}
												
						orderListMap.add(map);
					}

					Message msg = new Message();

					msg.what = 0;// ���ʳɹ����з���ֵ

					msg.obj = orderListMap;

					handler.sendMessage(msg);

				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 2;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	public void ViewOrderItem(String id) {

		Intent intent = new Intent();

		intent.putExtra("id", id);

		intent.setClass(OrderActivity.this, OrderDetailActivity.class);

		// ����Activity
		startActivity(intent);
	}
	 

	// Handler
	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			super.handleMessage(msg);

			switch (msg.what) {

			case 0:
				try {
					ArrayList<Map<String, Object>> result = (ArrayList<Map<String, Object>>) msg.obj;

					if (result != null) {				

						orderlist1.clear();

						orderlist1.addAll(result);

						adapter.notifyDataSetChanged();
						

						System.out.println(result.toString());
					}
				} catch (Exception ex) {

					System.out.println("ex: " + ex.getMessage());
				}
				break;

			case 2:

				Toast.makeText(getApplicationContext(), "����ʧ�ܣ���������!",
						Toast.LENGTH_SHORT).show();
				break;
			}

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.add_question, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class ClickEvent implements OnClickListener {

		@Override
		public void onClick(View v) {
			v.getTag();
		}

	}
}
